#include <cstdio>
#include <queue>

using namespace std;

struct Patient
{
    int id, yx;
};

bool operator < ( Patient a, Patient b )
{
    if ( a.yx < b.yx )
        return true;
    if ( a.yx == b.yx && a.id > b.id )
        return true;
    return false;
}

int main( )
{
    int n, t, i, y;
    char command[ 4 ];
    Patient p;
    priority_queue<Patient> q[ 3 ];
    while ( scanf("%d", &n) != EOF )
    {
        t = 1;
        for ( i = 0; i < 3; i++ )
            while ( !q[ i ].empty( ) )
                q[ i ].pop( );
        while ( n-- )
        {
            scanf("%s", command);
            if ( command[ 0 ] == 'I' )
            {
                scanf("%d%d", &y, &p.yx);
                p.id = t++;
                y--;
                q[ y ].push( p );
            }
            else
            {
                scanf("%d", &y);
                y--;
                if ( q[ y ].empty( ) )
                    printf("EMPTY\n");
                else
                {
                    printf("%d\n", q[ y ].top( ).id);
                    q[ y ].pop( );
                }
            }
        }
    }
    return 0;
}
